package com.abredatos.test;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts.Data;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.abredatos.R;
import com.abredatos.db.DBAdapter;
import com.abredatos.foo.Cheeses;
import com.abredatos.Main;

public class EfficientAdapterFromDatabase extends ListActivity {
	 private DBAdapter dbAdapter;
	 private static class EfficientAdapter extends BaseAdapter {
	        private LayoutInflater mInflater;
	        private Bitmap mIcon1;
	        private Bitmap mIcon2;

	        public EfficientAdapter(Context context) {
	            // Cache the LayoutInflate to avoid asking for a new one each time.
	            mInflater = LayoutInflater.from(context);

	            // Icons bound to the rows.
	            mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.cat105);
	            mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.cat101);
	        }

	        /**
	         * The number of items in the list is determined by the number of speeches
	         * in our array.
	         *
	         * @see android.widget.ListAdapter#getCount()
	         */
	        public int getCount() {
	            return DATA.length;
	        }

	        /**
	         * Since the data comes from an array, just returning the index is
	         * sufficent to get at the data. If we were using a more complex data
	         * structure, we would return whatever object represents one row in the
	         * list.
	         *
	         * @see android.widget.ListAdapter#getItem(int)
	         */
	        public Object getItem(int position) {
	            return position;
	        }

	        /**
	         * Use the array index as a unique id.
	         *
	         * @see android.widget.ListAdapter#getItemId(int)
	         */
	        public long getItemId(int position) {
	            return position;
	        }

	        /**
	         * Make a view to hold each row.
	         *
	         * @see android.widget.ListAdapter#getView(int, android.view.View,
	         *      android.view.ViewGroup)
	         */
	        public View getView(int position, View convertView, ViewGroup parent) {
	            // A ViewHolder keeps references to children views to avoid unneccessary calls
	            // to findViewById() on each row.
	            ViewHolder holder;

	            // When convertView is not null, we can reuse it directly, there is no need
	            // to reinflate it. We only inflate a new View when the convertView supplied
	            // by ListView is null.
	            if (convertView == null) {
	                convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

	                // Creates a ViewHolder and store references to the two children views
	                // we want to bind data to.
	                holder = new ViewHolder();
	                holder.text = (TextView) convertView.findViewById(R.id.text);
	                holder.icon = (ImageView) convertView.findViewById(R.id.icon);

	                convertView.setTag(holder);
	            } else {
	                // Get the ViewHolder back to get fast access to the TextView
	                // and the ImageView.
	                holder = (ViewHolder) convertView.getTag();
	            }

	            // Bind the data efficiently with the holder.
	            holder.text.setText(DATA[position]);
	            holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

	            return convertView;
	        }

	        static class ViewHolder {
	            TextView text;
	            ImageView icon;
	        }
	    }

	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	       /* dbAdapter = new DBAdapter(this);
	        dbAdapter.open();
	        dbAdapter.initiateFooEntity();
	        Cursor cursor = dbAdapter.getAllEntities();
	      //  dbAdapter.close();
	        /* Get the indices of the Columns we will need */
	        /*  int precioColumn = cursor.getColumnIndex("presupuesto");
            int fechaColumn = cursor.getColumnIndex("url");
            int infoColumn = cursor.getColumnIndex("proyecto");
            
            /* Check if our result was valid. */
	        /*   if (cursor != null) {
                    /* Check if at least one Result was returned. */
	        /*          if (cursor.isFirst()) {
                            int i = 0;
                            /* Loop through all Results */
	        /*                do {
                                   
                                    /* Retrieve the values of the Entry
                                     * the Cursor is pointing to. */
	        /*                      String firstName = cursor.getString(precioColumn);
                                    int age = cursor.getInt(fechaColumn);
                                    String descripcion = cursor.getString(infoColumn);
                                    
                                    /* We can also receive the Name
                                     * of a Column by its Index.
                                     * Makes no sense, as we already
                                     * know the Name, but just to shwo we can <img src="http://www.anddev.org/images/smilies/wink.png" alt=";)" title="Wink" /> */
                                    //String ageColumName = cursor.getColumnName(fechaColumn);
	        /*                  String infoColumnName =  cursor.getColumnName(infoColumn);
                  /*                  DATA[i] = infoColumnName;
                                    i++;
                                    /* Add current Entry to results. */
                                   // results.add("" + i + ": " + firstName
	        /*                  cursor.moveToNext();
                                   //                         + " (" + ageColumName + ": " + age + ")");
                            } while (!cursor.isLast());
                    }
            }
            dbAdapter.close();*/
	        for (int i=0; i < Main.obrasPlanE.size() ;i++) {
	        	String pruebas = Main.obrasPlanE.get(i).getUrlProyecto();
	        	String [] prueba  = pruebas.split(">");
	        	prueba = prueba[1].split("</a");
	        	pruebas = prueba[0];
	        	DATA[i] = pruebas;
	        }
	        	
            setListAdapter(new EfficientAdapter(this));
	    }
	    
	    private static final String[] DATA = new String[600] ;

}